#!python
import os
import logging
import sys
import platform

basedir=os.path.abspath(os.path.dirname(__file__))
sys.path.append(basedir)

from myshop import app

if platform.system() == "Windows":
    logdir = basedir
else:
    logdir = "/tmp"

logfile = os.path.join(logdir, "shop_error.log")

file_handler = logging.FileHandler(logfile)
file_handler.setFormatter(
        logging.Formatter(
            "%(asctime)s %(module)s:%(funcName)s:%(levelname)s) - %(message)s"
            )
        )
file_handler.setLevel(logging.DEBUG)

configfile = os.path.join(basedir, "appconfig.py")

if os.path.exists(configfile):
    print "found config %s" % configfile
    app.config.from_pyfile(configfile)
app.logger.addHandler(file_handler)
app.logger.setLevel(app.config["LOG_LEVEL"])

application = app

if __name__ == "__main__":
    from gevent.monkey import patch_all
    patch_all()
    from gevent.pywsgi import WSGIServer
    print "Listening on 0.0.0.0:5000"
    WSGIServer(('', 5000), app).serve_forever()
